package com.face.pass.active.utils;

import java.io.File;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.GetObjectRequest;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.region.Region;

public class QcloudUtils {
	// 客户端
	private static COSClient cosClient;
	// bucket的命名规则为{name}-{appid} ，此处填写的存储桶名称必须为此格式
	private static String bucketName = "active-1256669276";

	static {
		// 1 初始化用户身份信息(secretId, secretKey)
		COSCredentials cred = new BasicCOSCredentials("AKIDVuAF8pCiQvDHSWp8vbP2aEtOr9HFkc6c",
				"icfluztxtfFiVHNSaePFB0pT6lUKVfnx");
		// 2 设置bucket的区域, COS地域的简称请参照
		// https://cloud.tencent.com/document/product/436/6224
		// clientConfig中包含了设置region, https(默认http), 超时, 代理等set方法, 使用可参见源码或者接口文档FAQ中说明
		ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
		// 3 生成cos客户端
		cosClient = new COSClient(cred, clientConfig);
	}

	/**
	 * 上传文件
	 * 
	 * @param key 关键字
	 * @param localFile 本地文件
	 */
	public static void upload(String key, File localFile) {
		// 指定要上传到 COS 上对象键
		// 对象键（Key）是对象在存储桶中的唯一标识。例如，在对象的访问域名
		// `bucket1-1250000000.cos.ap-guangzhou.myqcloud.com/doc1/pic1.jpg` 中，对象键为
		// doc1/pic1.jpg, 详情参考
		// [对象键](https://cloud.tencent.com/document/product/436/13324)
		PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
		cosClient.putObject(putObjectRequest);
	}
	

	/**
	 * 下载文件
	 * 
	 * @param key 关键字
	 * @param downFile 目标文件
	 */
	public static void download(String key, File downFile) {
		// 指定要下载的文件所在的 bucket 和对象键
		GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
		cosClient.getObject(getObjectRequest, downFile);
	}

}
